﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Orid.Management.Statistics.Tools.Decomposition.Model;
using System.Data.SqlClient;
using Orid.Data.SqlServer;

namespace Orid.Management.Statistics.Tools.Decomposition.Data
{
    class ParentData:DataBase
    {
        public virtual List<ModelParentObject> GetParentDatas(Guid OrderId)
        {
            return null;
        }
       
        protected    List<ModelParentObject> GetParentDatas
            (
            Guid OrderId ,string connectString, string tableName
            )
        {
            List<ModelParentObject> listParentData = new List<ModelParentObject>();

            try
            {

                if (OrderId == null)
                {
                    return null;
                }
                 

                StringBuilder sqlText = new StringBuilder();
                sqlText.Append(" SELECT	 [ParentDataID],[OrderID] ,[ChildDataCount] AS [FrameCount],[UpdateDateTime] AS [CreatedDateTime],[FileName]");
                sqlText.Append("  FROM  " + tableName + "[ParentDatas]  with(NOLOCK)");//readpast
                sqlText.Append(" WHERE [OrderID] = @OrderID");

                using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connectString))
                {
                    conn.Open();
                    System.Data.SqlClient.SqlParameter[] SqlParameter;
                    SqlParameter = new System.Data.SqlClient.SqlParameter[]
                                 {     
                                       new System.Data.SqlClient.SqlParameter("@OrderID",System.Data.SqlDbType.UniqueIdentifier), 

                                 };

                    SqlParameter[0].Value = OrderId;

                    SqlDataReader orderReader = SqlHelper.ExecuteReader(conn, System.Data.CommandType.Text, sqlText.ToString(), SqlParameter);
                    listParentData = InitParentDataModel(orderReader);
                    if (conn.State == System.Data.ConnectionState.Open)
                        conn.Close();


                }
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                throw (new Exception(ex.Message));
            }

            return listParentData;
        }
        
        protected List<ModelParentObject> InitParentDataModel(SqlDataReader reader)
        {
            List<ModelParentObject> listModel = new List<ModelParentObject>();

            try
            {
                //[ParentDataID],[OrderID] ,[ChildDataCount] AS [FrameCount],[UpdateDateTime] AS [CreatedDateTime]
                while (reader.Read())
                {
                    ModelParentObject model = new ModelParentObject();

                    model.ParentDataID = (Guid)reader["ParentDataID"];
                    model.OrderID = (Guid)reader["OrderID"];
                    if (reader["FrameCount"] != DBNull.Value)
                    {
                        model.FrameCount = (int)reader["FrameCount"];

                    }

                    if (reader["CreatedDateTime"] != DBNull.Value)
                    {
                        model.CreatedDateTime = (DateTime)reader["CreatedDateTime"];

                    }
                    if (reader["FileName"] != DBNull.Value)
                    {
                        model.FileName = reader["FileName"].ToString();
                     
                    }

                    listModel.Add(model);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return listModel;
        }
    }
}
